package com.dc.datacollect.controller;

import com.dc.datacollect.conduit.R;
import com.dc.datacollect.dto.input.CreateGroupItemInput;
import com.dc.datacollect.dto.input.RenameGroupInput;
import com.dc.datacollect.dto.output.FindGroupItemOutput;
import com.dc.datacollect.service.GroupItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/groupItem")
public class GroupItemController {
    @Autowired
    private GroupItemService groupItemService;

    @PostMapping("/create")
    public R create(@Valid @RequestBody CreateGroupItemInput groupItemInput) throws Exception {
        String code = groupItemService.createGroupItem(groupItemInput);
        return R.ok("创建组成功").data(code);
    }
    @PostMapping("/remove/{groupItemId}")
    public R remove(@NotNull @PathVariable("groupItemId") Long groupItemId) {
        return R.auto("删除收集组", this.groupItemService.removeGroupsById(groupItemId));
    }

    @GetMapping("/find")
    public R find(@RequestParam("code") String code) {
        FindGroupItemOutput findGroupItemOutput = groupItemService.find(code);
        return R.ok().data(findGroupItemOutput);
    }
    @PostMapping("/joinGroup/{groupItemId}")
    public R joinGroup(@NotNull @PathVariable("groupItemId") Long groupItemId) throws Exception {
        Boolean isSuccess = groupItemService.joinGroup(groupItemId);
        return R.auto("加入收集组",isSuccess);
    }
    @PostMapping("/rename")
    public R rename(@Valid @RequestBody RenameGroupInput renameGroupInput) throws Exception {
        Boolean isSuccess = groupItemService.rename(renameGroupInput);
        return R.auto("重命名组",isSuccess);
    }
    @GetMapping("/getInvitationCode")
    public R rename(@NotNull @RequestParam("groupId") Long groupId) throws Exception {
        String code = groupItemService.genInviteCode(groupId);
        return R.ok("获取组邀请码").data(code);
    }


}
